package cc.rengu.igas.channel.cups.core.service.trans;

import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BasePayService;
import cc.rengu.igas.channel.cups.facade.request.DepositRequest;
import cc.rengu.igas.channel.cups.facade.response.DepositResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.igas.share.facade.bean.cups.F0_MTIEnum;
import cc.rengu.igas.share.facade.bean.cups.F25_ServiceConditionEnum;
import cc.rengu.igas.share.facade.bean.cups.F3_ProcessingCodeEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.BeanUtil;

/**
 * 功能描述：受理侧存款
 *
 * @author wangyongshuai
 * @date 2020/11/11
 */
public class CupsDepositService extends BasePayService<DepositRequest, DepositResponse> {
    @Override
    protected ChnlBizResponse<DepositResponse> callBizService(CupsFieldBean cupsFieldBean, DepositRequest depositRequest, DepositResponse depositResponse) throws Exception {
        CupsField cupsField = cupsFieldBean.getCupsField();
        cupsFieldBean.setCupsField(cupsField);
        cupsField.setMsgType(F0_MTIEnum.REQ_0200.getCode());
        cupsField.setProcCode(F3_ProcessingCodeEnum._210000.getCode());
        cupsField.setConCode(F25_ServiceConditionEnum._00.getCode());
        cupsField.setOrderAmt(AmountUtil.changeY2F(cupsField.getOrderAmt()));
        //  终端读取能力
        //  IC卡条件代码
        //  小额免密标志
        //  终端类型
        cupsFieldBean.createF60()
                .setF60_2_2(depositRequest.getTermRead())
                .setF60_2_3(depositRequest.getIcConCode())
                .setF60_2_6(depositRequest.getNoPinFlag())
                .setF60_2_5(depositRequest.getTermInfo().getDeviceType());
        cupsFieldBean.build();

        //  调用 CUPS   TODO 此交易配置暂时有误 ,关联了行内交易码, 后续解耦
        CupsFieldRspBean cupsFieldRspBean = callCups(cupsFieldBean, "P01CF002");

        // 应答赋值
        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, depositResponse);
        depositResponse.setCupsRespCode(cupsFieldRsp.getRespCode());
        if (depositResponse.getSettleDate() != null) {
            depositResponse.setSettleDate(getCupsSettleDate(depositResponse.getSettleDate()));
        }
        if (CupsParamConstant.SUCCESS_CODE.equals(cupsFieldRsp.getRespCode())) {
            depositResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            depositResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        }
        return new ChnlBizResponse<>(depositResponse);
    }
}
